import request from '../request'

// 搜索建议数据结构
export interface SearchSuggestion {
  id: string
  type: 'file' | 'folder' | 'recent'
  name: string
  path: string
  icon: string
  size?: string
  modifiedAt?: string
  parentFolder?: string
  folderId?: string
}

// 搜索建议响应
export interface SearchSuggestionsResponse {
  suggestions: SearchSuggestion[]
  totalCount: number
  hasMore: boolean
}

// 搜索建议请求参数
export interface SearchSuggestionsParams {
  q: string
  limit?: number
  types?: ('file' | 'folder')[]
}

// 完整搜索请求参数
export interface SearchParams {
  q: string
  page?: number
  size?: number
  fileType?: string
  minSize?: number
  maxSize?: number
  startDate?: string
  endDate?: string
  folderId?: string
}

/**
 * 搜索API模块
 */
export const searchApi = {
  /**
   * 获取搜索建议
   * @param params 搜索参数
   */
  getSuggestions: (params: SearchSuggestionsParams): Promise<SearchSuggestionsResponse> => {
    return request.get('/search/suggestions', { params })
  },

  /**
   * 执行完整搜索
   * @param params 搜索参数
   */
  search: (params: SearchParams) => {
    return request.get('/search', { params })
  },

  /**
   * 获取搜索历史
   * @param limit 返回数量限制
   */
  getSearchHistory: (limit: number = 10) => {
    return request.get('/search/history', { params: { limit } })
  },

  /**
   * 保存搜索历史
   * @param keyword 搜索关键词
   */
  saveSearchHistory: (keyword: string) => {
    return request.post('/search/history', { keyword })
  },

  /**
   * 清除搜索历史
   */
  clearSearchHistory: () => {
    return request.delete('/search/history')
  },

  /**
   * 获取热门搜索
   * @param limit 返回数量限制
   */
  getPopularSearches: (limit: number = 5) => {
    return request.get('/search/popular', { params: { limit } })
  }
}
